/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-12     YogaX1       the first version
 */

#include <stdio.h>
#include <board.h>
#include <flashdb.h>
#include <database.h>

#ifndef ULOG_USING_SYSLOG
#define LOG_TAG              "database"
#define LOG_LVL              LOG_LVL_DBG
#include <ulog.h>
#else
#include <syslog.h>
#endif /* ULOG_USING_SYSLOG */

uint32_t huodao_x[DANYUAN_NUM][CENG_NUM][HUODAO_NUM] = { 0 };
uint32_t huodao_y[DANYUAN_NUM][CENG_NUM] = { 0 };

struct fdb_default_kv_node default_kv_table[] = { { "username", "refugeer", 0 }, { "version", "20230614", 0 }, };
struct fdb_kvdb kvdb = { 0 };
struct fdb_default_kv default_kv;

void param_init(fdb_kvdb_t kvdb)
{
    struct fdb_blob blob;
    char dy_ceng_y_name[20];
    char dy_ceng_hd_x_name[20];
    int i, j, k;
    uint32_t value = 0;

//    for (i = 0; i < DANYUAN_NUM; i++)
//    {
//        for (j = 0; j < CENG_NUM; j++)
//        {
//            rt_snprintf(dy_ceng_y_name, sizeof(dy_ceng_y_name), "dy%d_ceng%d_y", i, j);
//            fdb_kv_get_blob(kvdb, dy_ceng_y_name, fdb_blob_make(&blob, &huodao_y[i][j], sizeof(huodao_y[i][j])));
//            if (blob.saved.len > 0)
//            {
//                LOG_D("%s=%d", dy_ceng_y_name, huodao_y[i][j]);
//            }
//            else
//            {
//                LOG_D("read %s failed!,try to make a new one", dy_ceng_y_name);
//                fdb_kv_set_blob(kvdb, dy_ceng_y_name, fdb_blob_make(&blob, &value, sizeof(value)));
//            }
//        }
//    }
//
//    for (i = 0; i < DANYUAN_NUM; i++)
//    {
//        for (j = 0; j < CENG_NUM; j++)
//        {
//            for (k = 0; k < HUODAO_NUM; k++)
//            {
//                rt_snprintf(dy_ceng_hd_x_name, sizeof(dy_ceng_hd_x_name), "dy%d_ceng%d_hd%d_x", i, j, k);
//                fdb_kv_get_blob(kvdb, dy_ceng_hd_x_name,
//                        fdb_blob_make(&blob, &huodao_x[i][j][k], sizeof(huodao_x[i][j][k])));
//                if (blob.saved.len > 0)
//                {
//                    LOG_D("%s=%d", dy_ceng_hd_x_name, huodao_x[i][j][k]);
//                }
//                else
//                {
//                    LOG_D("read %s failed!,try to make a new one", dy_ceng_hd_x_name);
//                    fdb_kv_set_blob(kvdb, dy_ceng_hd_x_name, fdb_blob_make(&blob, &value, sizeof(value)));
//                }
//            }
//        }
//    }
}

int database_init(void)
{
    fdb_err_t result;

    default_kv.kvs = default_kv_table;
    default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);

    /* Key-Value database initialization
     *
     *       &kvdb: database object
     *       "env": database name
     * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
     *              Please change to YOUR partition name.
     * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully.
     *        NULL: The user data if you need, now is empty.
     */
    result = fdb_kvdb_init(&kvdb, "database_param_config", "param", &default_kv, NULL);

    if (result != FDB_NO_ERR)
    {
        return -1;
    }

    param_init(&kvdb);

    return 0;
}
